package com.zbkj.service.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;

import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class DateTimeUtil {

    /**
     * 获取当天日期 数字类型
     *
     * @return {@link Integer}
     */
    public static Integer todayInt() {
        return Integer.valueOf(LocalDateTimeUtil.format(LocalDate.now(), DatePattern.PURE_DATE_PATTERN));
    }

    /**
     * 月初开始时间
     *
     * @return {@link Integer}
     */
    public static Integer monthBeginInt() {
        LocalDate monthBegin = LocalDate.now().withDayOfMonth(1);
        return Integer.valueOf(LocalDateTimeUtil.format(monthBegin, DatePattern.PURE_DATE_PATTERN));
    }

    public static Date localDateToDate(LocalDateTime time) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = time.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * 一周开始日期
     *
     * @return {@link Integer}
     */
    public static Integer weekStart() {
        LocalDate today = LocalDate.now();
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        String startDate = LocalDateTimeUtil.format(startOfWeek, DatePattern.PURE_DATE_PATTERN);
        return Integer.valueOf(startDate);
    }

    /**
     * 一周开始结束
     *
     * @return {@link Integer}
     */
    public static Integer weekEnd() {
        LocalDate today = LocalDate.now();
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        String endDate = LocalDateTimeUtil.format(endOfWeek, DatePattern.PURE_DATE_PATTERN);
        return Integer.valueOf(endDate);
    }

    /**
     * 月份开始日期
     *
     * @param month 月
     * @return {@link LocalDate}
     */
    public static LocalDate monthBeginDate(String month) {
        LocalDate monthDate = LocalDateTimeUtil.parse(month, DatePattern.NORM_MONTH_FORMATTER).toLocalDate();
        return monthDate.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 月结束日期
     *
     * @param month 月
     * @return {@link LocalDate}
     */
    public static LocalDate monthEndDate(String month) {
        LocalDate monthDate = LocalDateTimeUtil.parse(month, DatePattern.NORM_MONTH_FORMATTER).toLocalDate();
        return monthDate.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 月份开始时间
     *
     * @param month 月
     * @return {@link LocalDateTime}
     */
    public static LocalDateTime monthBeginTime(String month) {
        return monthBeginDate(month).atStartOfDay();
    }

    public static LocalDateTime monthBeginTime() {
        return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
    }

    /**
     * 月结束时间
     *
     * @param month 月
     * @return {@link LocalDateTime}
     */
    public static LocalDateTime monthEndTime(String month) {
        return LocalDateTime.of(monthEndDate(month), LocalTime.MAX);
    }

    public static LocalDateTime monthEndTime() {
        return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
    }

    public static Integer monthBegin(LocalDate date) {
        LocalDate monthBegin = date.withDayOfMonth(1);
        return Integer.valueOf(LocalDateTimeUtil.format(monthBegin, DatePattern.PURE_DATE_PATTERN));
    }

    public static Integer monthEnd(LocalDate date) {
        LocalDate monthEnd = date.withDayOfMonth(date.lengthOfMonth());
        return Integer.valueOf(LocalDateTimeUtil.format(monthEnd, DatePattern.PURE_DATE_PATTERN));
    }

}
